package model;

import com.example.menhoo.toollibrary.common.helper.DateUtil;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.Transient;
import com.lidroid.xutils.util.LogUtils;

import org.json.JSONObject;

import java.util.Date;
import java.util.UUID;

import application.AppApplication;

/**
 * Created by yanglei on 15/11/8.
 */
public class IMMessageDBModel {
    @Id
    public String UID;
    public String ID;
    public String MessageType;
    public String Context;
    public String SenderType;
    public String GroupID;
    public Date CreateTime=new Date();
    public String SenderID;
    public String Device;
    public String ImgUrl;
    public String DecContext;
    public String CreateTimeToString;
    public String CreateTimeToShortString;
    public String ShortTime;
    public String SendStatus;
    public String SenderName;
    @Transient
    public boolean isShowTime=true;

    //当前用户
    public long Progress=0;
    public String CurrUserID;

    public IMMessageDBModel(JSONObject data){
        try {
            this.UID = UUID.randomUUID().toString();
            this.ID = data.getString("ID");
            this.MessageType = data.getString("MessageType");
            this.Context = data.getString("Context");
            this.SenderType = data.getString("SenderType");
            this.GroupID = data.getString("GroupID");
            this.CreateTime = DateUtil.StringToDate(data.getString("CreateTimeToString"));
            this.SenderID = data.getString("SenderID");
            this.Device = data.getString("Device");
            this.ImgUrl = data.getString("ImgUrl");
            this.DecContext = data.getString("DecContext");
            this.CreateTimeToString = data.getString("CreateTimeToString");
            this.CreateTimeToShortString = data.getString("CreateTimeToShortString");
            this.ShortTime = data.getString("ShortTime");
            this.SendStatus = data.getString("SendStatus");
            this.SenderName = data.getString("SenderName");
            this.CurrUserID= AppApplication.getUser().EmplID;
        }
        catch (Exception ex){
            LogUtils.e(ex.getMessage()+"");
        }

    }
    public IMMessageDBModel(){
        this.UID = UUID.randomUUID().toString();
    }

    public IMFileModel getFileInfo(){
        if(this.MessageType.equalsIgnoreCase("File")){
            try{
                JSONObject json = new JSONObject(this.DecContext);
                String ID = json.getString("ID");
                String MD5 = json.getString("MD5");
                String Name = json.getString("Name");
                int Size = json.getInt("Size");
                String Ext = json.getString("Ext");
                int Status = json.getInt("Status");
                int Percentage = json.getInt("Percentage");

                IMFileModel file = new IMFileModel();
                file.ID=ID;
                file.MD5=MD5;
                file.Name=Name;
                file.Size=Size;
                file.Ext = Ext;
                file.Status =Status;
                file.Percentage = Percentage;

                boolean hasInfo = json.isNull("Info");
                if(!hasInfo){
                    JSONObject info = json.getJSONObject("Info");
                    int Width = info.getInt("Width");
                    int Height = info.getInt("Height");

                    int RealWidth = Width;
                    int RealHeight = Height;
                    try{
                        RealWidth = info.getInt("RealWidth");
                        RealHeight = info.getInt("RealHeight");
                    }
                    catch (Exception e){
                        LogUtils.e(e.getMessage()+"");
                    }


                    IMFileModel.ImgInfo imgInfo =  file.new ImgInfo();
                    imgInfo.Width = Width;
                    imgInfo.Height = Height;
                    imgInfo.RealWidth = RealWidth;
                    imgInfo.RealHeight = RealHeight;
                    file.Info = imgInfo;
                }
                else{
                    file.Info = null;
                }

                return file;
            }
            catch (Exception e){
                LogUtils.e(e.getMessage()+"");
            }
            return null;
        }
        return null;
    }
}
